﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PolePosition.v3.EntityLibrary;

namespace PolePosition.v3.Desktop.Editors
{
    public partial class DeployToCard : DialogBase
    {

        private string m_DestinationFolder;
        private int m_ProjectType;
        private int m_ProjectId;


        public DeployToCard()
        {
            InitializeComponent();

            this.Disposed += new EventHandler(DeployToCard_Disposed);
            DesktopFactory.MobileSetup.DownloadApplicationFilesComplete += new EventHandler(MobileSetup_DownloadApplicationFilesComplete);
            DesktopFactory.MobileSetup.DownloadApplicationFilesProgessChanged += new EventHandler<PolePosition.v3.DesktopFactory.MobileSetup.DownloadFilesProgressChangedEventArgs>(MobileSetup_DownloadApplicationFilesProgessChanged);

            m_ProjectType = Program.UserSelections.VendorSelectedProject.ProjectTypeFk.Value;
            m_ProjectId = Program.UserSelections.VendorSelectedProject.Vendor_ProjectId.Value;
        }

        private void DeployToCard_Disposed(object sender, EventArgs e)
        {
            this.Disposed -= new EventHandler(DeployToCard_Disposed);
            DesktopFactory.MobileSetup.DownloadApplicationFilesComplete -= new EventHandler(MobileSetup_DownloadApplicationFilesComplete);
            DesktopFactory.MobileSetup.DownloadApplicationFilesProgessChanged -= new EventHandler<PolePosition.v3.DesktopFactory.MobileSetup.DownloadFilesProgressChangedEventArgs>(MobileSetup_DownloadApplicationFilesProgessChanged);

        }

        private void buttonXOk_Click(object sender, EventArgs e)
        {

            if (inspectorSelector1.SelectedInspectors == null || inspectorSelector1.SelectedInspectors.Count() == 0)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Please select an inspector.", "Inspector", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            FolderBrowserDialog _FolderDialog = new FolderBrowserDialog();
            _FolderDialog.ShowNewFolderButton = true;
            if (!string.IsNullOrEmpty(Program.UserSelections.VendorSDPath))
                _FolderDialog.SelectedPath = Program.UserSelections.VendorSDPath;

            DialogResult _DialogResult = _FolderDialog.ShowDialog();

            if (_DialogResult != DialogResult.OK)
                return;

            buttonXOk.Enabled = false;
            base.ShowProgressPanel = true;
            base.StartProgressIndicator("Download");

            m_DestinationFolder = _FolderDialog.SelectedPath;
            Program.UserSelections.VendorSDPath = m_DestinationFolder;

            if (!DesktopFactory.MobileSetup.PrerequisiteFilesComplete)
            {
                DesktopFactory.MobileSetup.DownloadPrerequisiteFilesComplete += new EventHandler(MobileSetup_DownloadPrerequisiteFilesComplete);
                DesktopFactory.MobileSetup.DownloadPrerequisiteFilesProgessChanged += new EventHandler<DesktopFactory.MobileSetup.DownloadFilesProgressChangedEventArgs>(MobileSetup_DownloadPrerequisiteFilesProgessChanged);
                return;
            }
           
            lblPrerequisiteFilesProgress.Text = "COMPLETE";

            //_selectedDestinationDirectory = new DirectoryInfo(_FolderDialog.SelectedPath);
            DownloanApplicationFiles();

            //Note Create XML file w/ db that contains all information about deployment
            //project
            //inspector
            //type
            //date
            //vendor
            //utility
            //version
            //last load from card

        }

        private void MobileSetup_DownloadPrerequisiteFilesComplete(object sender, EventArgs e)
        {
            DesktopFactory.MobileSetup.DownloadPrerequisiteFilesComplete -= new EventHandler(MobileSetup_DownloadPrerequisiteFilesComplete);
            DesktopFactory.MobileSetup.DownloadPrerequisiteFilesProgessChanged -= new EventHandler<DesktopFactory.MobileSetup.DownloadFilesProgressChangedEventArgs>(MobileSetup_DownloadPrerequisiteFilesProgessChanged);

            MethodInvoker _ThreadSafe = new MethodInvoker(delegate()
            {
	            lblPrerequisiteFilesProgress.Text = "COMPLETE";
                Application.DoEvents();
                DownloanApplicationFiles();
            });

            lblPrerequisiteFilesProgress.Invoke(_ThreadSafe);
        }
      
        private void DownloanApplicationFiles()
        {
            VendorContactEntity _Inspector;
            _Inspector = inspectorSelector1.SelectedInspectors[0];

            DesktopFactory.MobileSetup.DownloadApplicationFiles(m_ProjectType, Program.UserSelections.VendorSelectedProject.UtilityFk.Value, null, _Inspector.VendorFK.Value, null, m_ProjectId, null, _Inspector.Vendor_ContactId.Value, _Inspector.UserName);
        }

        private void MobileSetup_DownloadApplicationFilesComplete(object sender, EventArgs e)
        {
            MethodInvoker _ThreadSafe = new MethodInvoker(delegate()
            {
                lblApplicationFilesProgress.Text = "COMPLETE - Copying to Destination";
                Application.DoEvents();
                DesktopFactory.MobileSetup.DeployFiles(m_ProjectType, m_DestinationFolder);

                this.ShowProgressPanel = false;
                buttonXOk.Enabled = true;
                this.DialogResult = DialogResult.OK;
            });

            lblApplicationFilesProgress.Invoke(_ThreadSafe);

        } 
        
        private void MobileSetup_DownloadApplicationFilesProgessChanged(object sender, PolePosition.v3.DesktopFactory.MobileSetup.DownloadFilesProgressChangedEventArgs e)
        {

            MethodInvoker _ThreadSafe = new MethodInvoker(delegate()
            {
                
                pnlShowProgress.Visible = true;
                lblApplicationFilesProgress.Text = string.Format("{0}/{1} ({2}%)", e.FilesReceived.ToString(), e.TotalFilesToReceive.ToString(), e.ProgressPercentage);
                Application.DoEvents();
            });

            lblApplicationFilesProgress.Invoke(_ThreadSafe);
        }

        private void MobileSetup_DownloadPrerequisiteFilesProgessChanged(object sender, PolePosition.v3.DesktopFactory.MobileSetup.DownloadFilesProgressChangedEventArgs e)
        {

            MethodInvoker _ThreadSafe = new MethodInvoker(delegate()
            {
                pnlShowProgress.Visible = true;

                lblPrerequisiteFilesProgress.Text = string.Format("{0}/{1} ({2}%)", e.FilesReceived.ToString(), e.TotalFilesToReceive.ToString(), e.ProgressPercentage);
                lblApplicationFilesProgress.Text = "NOT STARTED";
                Application.DoEvents();
            });

            lblApplicationFilesProgress.Invoke(_ThreadSafe);
        }
    }
}
